package com.polaris.live.ui.user.task

import android.os.Bundle
import com.blankj.utilcode.util.ColorUtils
import com.gyf.immersionbar.ImmersionBar
import com.polaris.live.R
import com.polaris.live.common.mvvm.activity.showLoading
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityTaskCenterBinding
import com.polaris.live.translation.R.string
import com.polaris.live.ui.login.BaseGoogleLoginActivity
import com.polaris.live.utils.fragment.attachFragment
import com.polaris.live.viewmodel.TaskModel

/**
 * TaskCenterActivity任务中心
 * @author Created by 天晴 on 2024/10/26 15:07
 * @since 1.0.0
 **/
class TaskCenterActivity : BaseGoogleLoginActivity<TaskModel, ActivityTaskCenterBinding>() {

    private var isBind = false
    private var waitTaskIds: List<Long>? = null //等待谷歌绑定成功再领取的任务id
    private var waitReceive: Boolean? = null //是否是领取用户任务

    private var waitActiveValue: Long = -1L //等待谷歌绑定成功再领取的活跃值
    private var waitDailyTask: Boolean = false //等待谷歌绑定成功再领取的周活跃还是日活跃

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBarMarginTop(mBinding.bvTitle)
            .navigationBarDarkIcon(true)
            .navigationBarColor(R.color.white)
            .transparentNavigationBar()
            .init()
    }

    private val mTaskCenterFragment by lazy {
        TaskCenterFragment().apply {
            setTopBack {
                setTitleScrollY(it)
            }
            setBindGoogleBack { ids, receive ->
                waitTaskIds = ids
                waitReceive = receive
                showLoading(string.google_bindding, cancelTime = 5000)
                launchGoogle()
            }

            setBindGoogleBackByActive { activeValue, dailyTask ->
                waitActiveValue = activeValue ?: -1L
                waitDailyTask = dailyTask ?: false
                showLoading(string.google_bindding, cancelTime = 5000)
                launchGoogle()
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        isBind = intent.getBooleanExtra("bind", false)
        intent.getLongArrayExtra("taskId")?.toList().apply {
            waitTaskIds = this
        }
        intent.getBooleanExtra("receiveUserReward", false).apply {
            waitReceive = this
        }
        intent.getLongExtra("activeValue", -1L).apply {
            waitActiveValue = this
        }
        intent.getBooleanExtra("dailyTask", false).apply {
            waitDailyTask = this
        }



        supportFragmentManager.attachFragment(R.id.fl_fragment) {
            mTaskCenterFragment
        }
        mViewModel.googleBindLiveData.observe(this) { state ->
            dismissLoading()
            parseState(state, {
                if (waitActiveValue != -1L) {
                    mTaskCenterFragment.bindSuccess(waitActiveValue, waitDailyTask)
                } else {
                    mTaskCenterFragment.bindSuccess(waitTaskIds, waitReceive)
                }
            }, {
                ToastGravity.showGravity(it.message)
            })
        }

        if (isBind) {
            showLoading(string.google_bindding, cancelTime = 5000)
            launchGoogle()
        }
    }

    override fun googleLoginSuccess(id: String, token: String) {
        mViewModel.googleBind(id, token)
    }

    private fun setTitleScrollY(scrollY: Int) {
        if (scrollY == 0) {
            mBinding.titleLl.setBackgroundColor(ColorUtils.getColor(R.color.color_transparent))
        } else {
            mBinding.titleLl.setBackgroundColor(ColorUtils.getColor(R.color.white))
        }

    }
}